package com.marvels.financial.loan.service.impl;
import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.consum.FinancialConsumerApproveApi;
import com.marvels.financial.loan.dto.LoanAuditConditionDto;
import com.marvels.financial.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.financial.loan.service.LoanAuditApi;
import com.rabbitmq.client.Channel;

/**
 * 自动放款信息
 * @return
 */
@Component
public class PullWaitLentInfoReceiver implements ChannelAwareMessageListener {
	
	@Autowired
	private FinancialConsumerApproveApi approveApi ;
	
	@Autowired
	private LoanAuditApi loanAuditApi;
	
//	@RabbitHandler
//	@RabbitListener(queues = PlatformConstant.PLATFORM_NAME_PREFIX + "time_pull_wait_lend_info")
//	public void process(String loanId,Message message,Channel channel){
//		try {
//			//判断当前loanId是否在待放款状态
//			McamLoanApplyInfoDTO mcamLoanApplyInfoDTO=new McamLoanApplyInfoDTO();
//			mcamLoanApplyInfoDTO.setLoanId(loanId);
//			mcamLoanApplyInfoDTO=approveApi.selectLoanApplyInfo(mcamLoanApplyInfoDTO);
//			if(mcamLoanApplyInfoDTO==null){
//				MarvelsLogUtil.getInstance().info(">>>>>>>>loanId"+loanId+"自动放款进件信息不存在");
//				return;
//			}
//			if(!mcamLoanApplyInfoDTO.getStatus().equals("P05")){
//				MarvelsLogUtil.getInstance().info(">>>>>>>>loanId"+loanId+"自动放款进件信息不在待放款状态");
//				return;
//			}
//			//执行放款操作
//			approveApi.loanOperation(loanId);
//			LoanAuditConditionDto conditionDto=new LoanAuditConditionDto();
//			conditionDto.setOperationStatus("1");
//			conditionDto.setLoanIds(loanId);
//			conditionDto.setLendType("2");//放款类型   1 人工  2自动
//			try {
//				//执行审批通过字段
//				loanAuditApi.executeApplyAudist(conditionDto);
//			} catch (MarvelsBusinessException e) {
//				MarvelsLogUtil.getInstance().error(">>>>>>>>自动放款审核通过失败");
//			}
//			//ack通知MQ处理成功
//			channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//		} catch (Exception e) {
//			MarvelsLogUtil.getInstance().error(">>>>>loanId:"+loanId+"自动放款信息处理失败");
//			try {
//				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//			} catch (IOException e1) {
//				MarvelsLogUtil.getInstance().error(">>>>>loanId:"+loanId+"自动放款信息处理mq队列销毁失败");
//			}
//		}
//	}

	@Override
	public void onMessage(Message message, Channel channel) throws Exception {
		MarvelsLogUtil.getInstance().info("PullWaitLentInfoReceiver.onMessage message:" + message.toString());
        String loanId  = new String(message.getBody());
        MarvelsLogUtil.getInstance().info("PullWaitLentInfoReceiver.onMessage loanId:" + loanId);
		try {
			//判断当前loanId是否在待放款状态
			McamLoanApplyInfoDTO mcamLoanApplyInfoDTO=new McamLoanApplyInfoDTO();
			mcamLoanApplyInfoDTO.setLoanId(loanId);
			mcamLoanApplyInfoDTO=approveApi.selectLoanApplyInfo(mcamLoanApplyInfoDTO);
			if(mcamLoanApplyInfoDTO==null){
				MarvelsLogUtil.getInstance().info(">>>>>>>>loanId"+loanId+"自动放款进件信息不存在");
				return;
			}
			if(!mcamLoanApplyInfoDTO.getStatus().equals("P05")){
				MarvelsLogUtil.getInstance().info(">>>>>>>>loanId"+loanId+"自动放款进件信息不在待放款状态");
				return;
			}
			//执行放款操作
			approveApi.loanOperation(loanId);
			LoanAuditConditionDto conditionDto=new LoanAuditConditionDto();
			conditionDto.setOperationStatus("1");
			conditionDto.setLoanIds(loanId);
			conditionDto.setLendType("2");//放款类型   1 人工  2自动
			try {
				//执行审批通过字段
				loanAuditApi.executeApplyAudist(conditionDto);
			} catch (MarvelsBusinessException e) {
				MarvelsLogUtil.getInstance().error(">>>>>>>>自动放款审核通过失败");
			}
			//ack通知MQ处理成功
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>loanId:"+loanId+"自动放款信息处理失败");
			try {
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
			} catch (IOException e1) {
				MarvelsLogUtil.getInstance().error(">>>>>loanId:"+loanId+"自动放款信息处理mq队列销毁失败");
			}
		}
	}
}
